'''
Created on Apr 16, 2012

@author: lavang
'''
import unittest
from selenium import webdriver
from Interface.administratorPage import xpathUserTypes
from Interface.userTypesPage import xpathNewUserTypes, xpathTxtTypeName,\
    xpathCbbBasicUserType, xpathCbbContent, xpathCbbUser, xpathCbbReport,\
    xpathCbbPersonalMessage, xpathCbbControlPanel, xpathCbbMoveBlock,\
    xpathCbbDashBoard, xpathCbbCalendar, xpathCbbSurvey, xpathCbbAnnouncement,\
    xpathCbbForum, xpathCbbLesson, xpathCbbFiles, xpathCbbUserStatus,\
    xpathCbbCourseOption, xpathCbbGlossary, xpathCbbLessionOption,\
    xpathCbbConfigurationOption, xpathCbbThemes, xpathCbbLogOutUser,\
    xpathCbbPublicProfile, xpathCbbLanguage, xpathCbbRegistrationKey,\
    xpathCbbMaintenance, xpathCbbBackupRestore, xpathCbbModules, xpathCbbArchive,\
    xpathCbbNotification, xpathCbbUserType,xpathCbbSetAllTo, xpathBtnSave,\
    xpathDelete
from selenium.webdriver.support.select import Select
from Functions.globalFunc import readData
from selenium.webdriver.support.wait import WebDriverWait


change=readData("view")
view=readData("view",index=2)
hide=readData("view",index=3)
def clickOnUserTypes(self):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathUserTypes())
    element.click()
    
def clickOnNewUserType(self):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathNewUserTypes())
    element.click()
    
# Create an User type

def createUserType(self,typeName,basicUserType='Student',setAll='yes',edit='off'):
    #browser=webdriver.Firefox()
    browser=self.browser
    # enter user type's name
    element=browser.find_element_by_xpath(xpathTxtTypeName())
    element.clear()
    element.send_keys(typeName)
    if edit=='off':
        # select basic type and its option
        element=browser.find_element_by_xpath(xpathCbbBasicUserType())
        Select(element).select_by_visible_text(basicUserType)
    else:
        pass
    if basicUserType=='Student':
        ################ Option of Student ##############################
        # select content
        element=browser.find_element_by_xpath(xpathCbbContent())
        Select(element).select_by_visible_text(change)
        # select user
        element=browser.find_element_by_xpath(xpathCbbUser())
        Select(element).select_by_visible_text(view)
        # select report
        element=browser.find_element_by_xpath(xpathCbbReport())
        Select(element).select_by_visible_text(hide)
        # select personalMSG
        element=browser.find_element_by_xpath(xpathCbbPersonalMessage())
        Select(element).select_by_visible_text(change)
        # select control panel
        element=browser.find_element_by_xpath(xpathCbbControlPanel())
        Select(element).select_by_visible_text(view)
        # select move block
        element=browser.find_element_by_xpath(xpathCbbMoveBlock())
        Select(element).select_by_visible_text(hide)
        # select dashboard
        element=browser.find_element_by_xpath(xpathCbbDashBoard())
        Select(element).select_by_visible_text(change)      
        # select calendar
        element=browser.find_element_by_xpath(xpathCbbCalendar())
        Select(element).select_by_visible_text(view)
        # select survey
        element=browser.find_element_by_xpath(xpathCbbSurvey())
        Select(element).select_by_visible_text(hide)
        # select announcement
        element=browser.find_element_by_xpath(xpathCbbAnnouncement())
        Select(element).select_by_visible_text(change)      
        # select forum
        element=browser.find_element_by_xpath(xpathCbbForum())
        Select(element).select_by_visible_text(view)
    elif basicUserType=='Professor':
        
        ######## Option of Professor ###################################
        
        # Lesson options
        element=browser.find_element_by_xpath(xpathCbbLessionOption())
        Select(element).select_by_visible_text(change)
        # Users
        element=browser.find_element_by_xpath(xpathCbbUser())
        Select(element).select_by_visible_text(view)      
        # content
        element=browser.find_element_by_xpath(xpathCbbContent())
        Select(element).select_by_visible_text(hide)
        # file
        element=browser.find_element_by_xpath(xpathCbbFiles())
        Select(element).select_by_visible_text(change)
        # user status
        element=browser.find_element_by_xpath(xpathCbbUserStatus())
        Select(element).select_by_visible_text(view)
        # report
        element=browser.find_element_by_xpath(xpathCbbReport())
        Select(element).select_by_visible_text(hide)
        # personal message
        element=browser.find_element_by_xpath(xpathCbbPersonalMessage())
        Select(element).select_by_visible_text(change)   
        # control panel
        element=browser.find_element_by_xpath(xpathCbbControlPanel())
        Select(element).select_by_visible_text(view)
        # dashboard
        element=browser.find_element_by_xpath(xpathCbbDashBoard())
        Select(element).select_by_visible_text(hide)
        # move block
        element=browser.find_element_by_xpath(xpathCbbMoveBlock())
        Select(element).select_by_visible_text(change)
        # courser option
        element=browser.find_element_by_xpath(xpathCbbCourseOption())
        Select(element).select_by_visible_text(view)
        # glossary
        element=browser.find_element_by_xpath(xpathCbbGlossary())
        Select(element).select_by_visible_text(hide)  
        # calendar
        element=browser.find_element_by_xpath(xpathCbbCalendar())
        Select(element).select_by_visible_text(view)
        # survey
        element=browser.find_element_by_xpath(xpathCbbSurvey())
        Select(element).select_by_visible_text(hide)
        # announcement
        element=browser.find_element_by_xpath(xpathCbbAnnouncement())
        Select(element).select_by_visible_text(change)      
        # forum
        element=browser.find_element_by_xpath(xpathCbbForum())
        Select(element).select_by_visible_text(view)
    ############## Option of Administrator ###############################
    elif basicUserType=='Administrator': 
        # User
        element=browser.find_element_by_xpath(xpathCbbUser())
        Select(element).select_by_visible_text(view)    
        # configuration option
        element=browser.find_element_by_xpath(xpathCbbConfigurationOption())
        Select(element).select_by_visible_text(view)
        # theme
        element=browser.find_element_by_xpath(xpathCbbThemes())
        Select(element).select_by_visible_text(view)
        # log out user
        element=browser.find_element_by_xpath(xpathCbbLogOutUser())
        Select(element).select_by_visible_text(view)
        # public profile
        element=browser.find_element_by_xpath(xpathCbbPublicProfile())
        Select(element).select_by_visible_text(view)
        # user type
        element=browser.find_element_by_xpath(xpathCbbUserType())
        Select(element).select_by_visible_text(view)
        # language
        element=browser.find_element_by_xpath(xpathCbbLanguage())
        Select(element).select_by_visible_text(view)
        # registration key
        element=browser.find_element_by_xpath(xpathCbbRegistrationKey())
        Select(element).select_by_visible_text(view)
        # maintenance
        element=browser.find_element_by_xpath(xpathCbbMaintenance())
        Select(element).select_by_visible_text(view)
        # backup restore
        element=browser.find_element_by_xpath(xpathCbbBackupRestore())
        Select(element).select_by_visible_text(view)
        # module
        element=browser.find_element_by_xpath(xpathCbbModules())
        Select(element).select_by_visible_text(view)
        # report
        element=browser.find_element_by_xpath(xpathCbbReport())
        Select(element).select_by_visible_text(view)
        # archive
        element=browser.find_element_by_xpath(xpathCbbArchive())
        Select(element).select_by_visible_text(view)
        # personal msg
        element=browser.find_element_by_xpath(xpathCbbPersonalMessage())
        Select(element).select_by_visible_text(view)
        # notification
        element=browser.find_element_by_xpath(xpathCbbNotification())
        Select(element).select_by_visible_text(view)
        # control panel
        element=browser.find_element_by_xpath(xpathCbbControlPanel())
        Select(element).select_by_visible_text(view)
        # dashboard
        element=browser.find_element_by_xpath(xpathCbbDashBoard())
        Select(element).select_by_visible_text(hide)
        # calendar
        element=browser.find_element_by_xpath(xpathCbbCalendar())
        Select(element).select_by_visible_text(view)
        # announcement
        element=browser.find_element_by_xpath(xpathCbbAnnouncement())
        Select(element).select_by_visible_text(change)      
        # forum
        element=browser.find_element_by_xpath(xpathCbbForum())
        Select(element).select_by_visible_text(view)
    else:
        pass
    ## See all
    element=browser.find_element_by_xpath(xpathCbbSetAllTo())
    if setAll=='yes':
        Select(element).select_by_visible_text(view)
    else:
        pass
    # Click Save button
    element=browser.find_element_by_xpath(xpathBtnSave())
    element.click()
    
# Verify that an User type is created successfully
def isUserTypeCreatedSuccessfully(self,userTypeName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[contains(text(),'"+userTypeName+"')]"))
        return True
    except:
        return False
    
# delete User Type
def deleteUserType(self,userTypeName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathDelete(userTypeName))
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify that user type is deleted successfully
def isUserTypeDeleted(self,userTypeName):
    browser=self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser:browser.fin_element_by_xpath("//a[contains(text(),'"+userTypeName+"')]"))
        return True
    except:
        return False
    
# click on an user type
def clickOnUserType(self,userTypeName):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+userTypeName+"')]")
    element.click()